$(document).ready(function() {
	var VAL = {
		NORMAL : "0000000",
		CHN_LVL : {
			NORMAL : "正常",
			WARNING : "警告",
			DANGER : "严重"
		},
		CSS_LVL : {

		},
		DEAL_RLT : {
			SUCCESS : "成功",
			FAILED : "失败"
		}
	};
	var domObj = {
		$f1ShowStatus : $("#f1ShowStatus"),
		$statusTbl : $("#statusTbl"),
		$dealRtnMesg : $("#dealRtnMesg"),
		$dealErrorInfoTbl : $("#dealErrorInfoTbl")
	};
	
	var comFuncs = {

		getLevel : function(queueName, depth) {
			var chnLevel;
			if (this.isErrorQueue(queueName)) {
				if (depth > 100) {
					chnLevel = VAL.CHN_LVL.DANGER;
				} else if (depth > 1 && depth < 100) {
					chnLevel = VAL.CHN_LVL.WARNING;
				} else {
					chnLevel = VAL.CHN_LVL.NORMAL;
				}
			} else {
				if (depth > 3000) {
					chnLevel = VAL.CHN_LVL.DANGER;
				} else if (depth > 1500 && depth < 3000) {
					chnLevel = VAL.CHN_LVL.WARNING;
				} else {
					chnLevel = VAL.CHN_LVL.NORMAL;
				}
			}
			return chnLevel;
		},
		setLevelCss : function(level) {
			var trCss;
			switch (level) {
				case VAL.CHN_LVL.NORMAL :
					trCss = "success";
					break;
				case VAL.CHN_LVL.WARNING :
					trCss = "warning";
					break;
				case VAL.CHN_LVL.DANGER :
					trCss = "danger";
					break;
				default :
					trCss = "success";
			}

			return trCss;
		},
		isErrorQueue : function(queue) {
			return queue.indexOf(".ERR") === -1 ? false : true;
		},
		fillStatusTable : function(tableObjectArray, $tableDom) {
			var str = "", i, tableObject, queueName, depth, level, trCss;
			if (tableObjectArray == null) {
				return;
			}
			for (i = 0; i < tableObjectArray.length; i++) {
				tableObject = tableObjectArray[i];

				queueName = tableObject["queueName"];
				depth = tableObject["depth"];
				level = this.getLevel(queueName, depth);
				trCss = this.setLevelCss(level);

				str += "<tr class=" + trCss + ">";
				str += "<td>" + queueName + "</td>";
				str += "<td>" + depth + "</td>";
				str += "<td>" + level + "</td>";
				str += "<td><button class='exportCss btn btn-large btn-success'>导出</button></td>";

				if (this.isErrorQueue(queueName)) {
					str += "<td><button class='dealErrorCss btn btn-large btn-info'>抄送</button></td>";
				} else {
					str += "<td></td>"
				}

				str += "</tr>";
			}
			$tableDom.children("tbody").children("tr:has(td)").remove();
			$tableDom.append($(str));
			$(".exportCss").bind("click", btnFuncs.export2File);
			$(".dealErrorCss").bind("click", btnFuncs.dealErrorQueue);
		},

		fillDealErrorInfoTbl : function(tableObjectArray, $tableDom) {
			var str = "", i, tableObject, queueName;
			if (tableObjectArray == null) {
				return;
			}
			for (i = 0; i < tableObjectArray.length; i++) {
				tableObject = tableObjectArray[i];
				if (tableObject["is_success"] === "成功") {
					str += "<tr class='success'>";
				} else {
					str += "<tr class='danger'>";
				}
				for (var property in tableObject) {
					if (tableObject.hasOwnProperty(property)) {
						str += "<td>" + tableObject[property] + "</td>";
					}
				}
				str += "</tr>";
			}
			$tableDom.children("tbody").children("tr:has(td)").remove();
			$tableDom.append($(str));
		},

		showSaveFileDialog : function(queueName) {
			var formId = "form-" + new Date().getTime();
			var dataId = formId + "-data";
			var html = '<form id="'
					+ formId
					+ '" style="position:absolute;z-index:-1122;" method="post" action="rest/export2File.do">';
			html += '<input id="queueNameTxt" name="queueName" type="hidden">';
			html += '</form>';

			if ($("#" + formId).length > 0) {
				$("#" + formId).remove();
			}

			$(html).appendTo("body");
			$("#queueNameTxt").val(queueName);
			$("#" + formId).submit();
			$("#" + formId).remove();

		},
		showReturnMessage : function(message, outObj) {

			if (outObj.rtn_code === VAL.NORMAL) {
				domObj.$dealRtnMesg.removeClass("alert-danger");
				domObj.$dealRtnMesg.addClass("alert-success");
			} else {
				domObj.$dealRtnMesg.removeClass("alert-success");
				domObj.$dealRtnMesg.addClass("alert-danger");
			}
			domObj.$dealRtnMesg.html(message);
		}
	};

	var btnFuncs = {
		showStatus : function(isShowLog) {
			var outObj;
			$.ajax({
						type : "POST",
						url : "rest/showStatus.do",
						async : false,
						dataType : 'json',
						success : function(data) {
							var outObj = data, message;

							if (outObj.rtn_code === VAL.NORMAL) {
								comFuncs.fillStatusTable(data.oary,
										domObj.$statusTbl, true);
							} else {
								showMessengerErrorDialog("请求查看状态失败,请查看log");
							}

							if (typeof(isShowLog) === "undefined" || isShowLog) {
								message = "<H1>";
								message += outObj.rtn_code === VAL.NORMAL
										? "显示状态成功"
										: "显示状态失败";
								message += "</H1><br/>";
								message += outObj.rtn_mesg;
								comFuncs.showReturnMessage(message, outObj);
							}
							console.info(data);
						}
					});
		},
		export2File : function() {
			var queueName = $(this).parent().siblings("td:eq(0)").text();
			var depth = $(this).parent().siblings("td:eq(1)").text();
			console.info(queueName + ":" + depth);
			if (!queueName) {
				showMessengerErrorDialog("队列名称为空,请确认");
				return false;
			}
			if (depth <= 0) {
				showMessengerErrorDialog("队列深度为零,无法导出");
				return false;
			}
			comFuncs.showSaveFileDialog(queueName);
			// 不用form提交的action,为什么不能返回文件流,而直接是json格式
			// $.ajax({
			// type : "POST",
			// url : "rest/export2File.do",
			// async : false,
			// dataType : 'json',
			// data : {queueName:queueName},
			// success : function(data) {
			// console.info("success export");
			// }
			// });

		},
		dealErrorQueue : function() {
			var queueName = $(this).parent().siblings("td:eq(0)").text();
			var depth = $(this).parent().siblings("td:eq(1)").text();
			if (!queueName) {
				showMessengerErrorDialog("队列名称为空,请确认");
				return false;
			}
			if (depth <= 0) {
				showMessengerErrorDialog("队列深度为零,无法抄送");
				return false;
			}
			$.ajax({
				type : "POST",
				url : "rest/dealErrorQueue.do",
				async : false,
				dataType : 'json',
				data : {
					queueName : queueName,
					fetchCnt : depth > 1000 ? 1000 : depth
				},
				success : function(data) {
					var outObj = data, message, dealCnt, i, oary, isSuccess;

					message = "<H1>";
					message += outObj.rtn_code === VAL.NORMAL ? "抄送成功" : "抄送失败";
					message += "</H1><br/>";
					message += "数量[" + outObj.success_cnt + "]:处理成功了";
					message += "<br/>"
					message += "数量[" + outObj.un_success_cnt + "]:处理失败了";

					comFuncs.showReturnMessage(message, outObj);

					dealCnt = outObj.deal_cnt;

					for (i = 0; i < dealCnt; i++) {
						oary = outObj.oary[i];
						isSuccess = oary.is_success;
						oary.is_success = isSuccess === true ? "成功" : "失败";
						oary.deal_mesg = isSuccess === true
								? "成功"
								: oary.deal_mesg;
					}

					comFuncs.fillDealErrorInfoTbl(outObj.oary,
							domObj.$dealErrorInfoTbl);
					console.info(data);

					btnFuncs.showStatus(false);
				}
			});
		}
	};

	domObj.$f1ShowStatus.click(function() {
				btnFuncs.showStatus();
				domObj.$dealErrorInfoTbl.children("tbody").children("tr:has(td)").remove();
			});
});
